import os
import re


def write_to_file(file_output_dir: str, file_name: str, content: str):
    if not os.path.exists(file_output_dir):
        os.mkdir(file_output_dir)
    with open(os.path.join(file_output_dir, file_name), 'w', encoding='utf8') as f:
        f.write(content)


def input_multilines(hint: str) -> str:
    print(hint)
    lines = []
    while True:
        line = input()
        if line == '@@@@@':
            break
        else:
            lines.append(line)
    text = '\n'.join(lines)
    return text


def puml_to_svg(puml_file_name: str):
    puml_suffix = '.puml'
    if not puml_file_name.endswith(puml_suffix):
        raise Exception('file extension is not puml')
    file_name = puml_file_name.removesuffix(puml_suffix)
    os.system(f'cat {puml_file_name} | java -jar plantuml-1.2024.4.jar -tsvg -pipe > {file_name}.svg')


def get_puml_from_markdown(markdown: str) -> str:
    m = re.search(r'```plantuml(.*)```', markdown, re.DOTALL | re.IGNORECASE)
    return m.groups()[0].strip()
